/*
 * ﻿Copyright (C) 2009-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.gwetons.client.ui;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;

/**
 * Provide appropriate tab-panel type via {@link TabPanelCase} interface based on 
 * {@link Document#getCompatMode()} value: either {@link TabLayoutPanel} for Standard mode or 
 * {@link DecoratedTabPanel} for Quirks mode. <br />
 * Since <code>TabPanelCase</code> interface provides common public methods between 
 * <code>TabLayoutPanel</code> and <code>DecoratedTabPanel</code>, using <code>TabPanelCase</code> 
 * interface eliminates the consideration for which type the underneath tab panel class object is. 
 * However, be advised that there are different behaviors between <code>TabLayoutPanel</code> and 
 * <code>DecoratedTabPanel</code> especially regarding style.
 * 
 * @author Arata Y.
 */
public class TabPanelCaseProvider extends Composite {
	public static class StandardModeTabPanel extends TabLayoutPanel implements TabPanelCase {
		public StandardModeTabPanel() {
			super( 1.5, Unit.EM);
		}
	} // class StandardModeTabPanel extends TabLayoutPanel implements TabPanelProviderService
	
	public static class QuirksModeTabPanel extends DecoratedTabPanel implements TabPanelCase {
		protected int selectedIndex = -1;
		
		public QuirksModeTabPanel() {
			super();
			this.addSelectionHandler( 
					new SelectionHandler<Integer>() {
						public void onSelection( SelectionEvent<Integer> event) {
							selectedIndex = event.getSelectedItem().intValue();
						}
					});
		}

		public int getSelectedIndex() {
			return selectedIndex;
		}
	} // class StandardModeTabPanel extends TabLayoutPanel implements TabPanelProviderService
	
	private TabPanelCase tabPanelCase;
		public TabPanelCase getTabPanelCase() {
			return tabPanelCase;
		}
		protected void setTabPanelCase( TabPanelCase tabPanelCase) {
			this.tabPanelCase = tabPanelCase;
		}

	public TabPanelCaseProvider() {
		if ( "BackCompat".equals( Document.get().getCompatMode())) {
			QuirksModeTabPanel tabPanel = new QuirksModeTabPanel();
			initWidget( tabPanel);
			tabPanelCase = tabPanel;
		}
		else {
			StandardModeTabPanel tabPanel = new StandardModeTabPanel();
			initWidget( tabPanel);
			tabPanelCase = tabPanel;
		}
	}	
}
